package com.apptest.android.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.apptest.android.R;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.apptest.android.core.bean.BannerBean;
import com.apptest.android.databinding.FragmentHomeBinding;
import com.apptest.android.ui.ArticleList.ArticleListAdapter;
import com.apptest.android.ui.common.CommonActivity;
import com.apptest.android.util.BannerGlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private FragmentHomeBinding binding;
    private ArticleListAdapter mAdapter;
    private Banner mBanner;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);

        initView();

        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void initView(){
        mAdapter = new ArticleListAdapter(
                R.layout.item_article_list, homeViewModel.getArticleItemList().getValue()
        );
        LinearLayout mHeaderGroup = (LinearLayout) getLayoutInflater().inflate(R.layout.item_header_banner, null);
        mBanner = mHeaderGroup.findViewById(R.id.item_header_banner);
        mHeaderGroup.removeView(mBanner);
        mAdapter.setHeaderView(mBanner);
        mAdapter.setOnItemClickListener((adapter, view, position) -> startCommonActivity(position));
        binding.homePagerRecyclerView.setAdapter(mAdapter);
        homeViewModel.getArticleItemList().observe(getViewLifecycleOwner(),
                list -> mAdapter.replaceData(list));
        binding.homePagerRecyclerView.setLayoutManager(new LinearLayoutManager(
                        getActivity(), LinearLayoutManager.VERTICAL, false
                ));
        initRefreshLayout();
        initBanner();
    }

    private void initRefreshLayout() {
        binding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            homeViewModel.updateArticleItemList(true);
            refreshLayout.finishRefresh();
        });
        binding.smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            homeViewModel.updateArticleItemList(false);
            refreshLayout.finishLoadMore();
        });
    }

    private void initBanner(){
        //设置banner样式
        mBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //设置图片加载器
        mBanner.setImageLoader(new BannerGlideImageLoader());
        //设置banner动画效果
        mBanner.setBannerAnimation(Transformer.Accordion);
        //设置轮播时间
        mBanner.setDelayTime(5000);
        //设置指示器位置（当banner模式中有指示器时）
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        homeViewModel.getBannerList().observe(getViewLifecycleOwner(), list -> {
            List<String> mBannerTitleList = new ArrayList<>();
            List<String> mBannerImageList = new ArrayList<>();
            for(BannerBean bannerBean: list){
                mBannerTitleList.add(bannerBean.getTitle());
                mBannerImageList.add(bannerBean.getImagePath());
            }
            //设置图片集合
            mBanner.setImages(mBannerImageList);
            //设置标题集合（当banner样式有显示title时）
            mBanner.setBannerTitles(mBannerTitleList);
            mBanner.start();
        });
    }

    private void startCommonActivity(int position){
        int mAdapterSize=mAdapter.getData().size();
        if (mAdapterSize <= 0 || mAdapterSize < position) {
            return;
        }
        CommonActivity.startCommonActivity(getContext(),
                mAdapter.getData().get(position).getTitle(),
                mAdapter.getData().get(position).getLink());
    }

}